/**********************************************************
 * ResendConfirmationAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:49:28 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.dto.MailingSystem;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;
import com.printeverywhere.util.MailHelper;

/**
 * The Class ResendConfirmationAction.
 */
public class ResendConfirmationAction extends AdminEngineManagement implements ServletRequestAware {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ResendConfirmationAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private int pendingCount;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		if(request.getSession().getAttribute("sendConfirmationMail") != null){
			List<User> list = (List<User>) request.getSession().getAttribute("sendConfirmationMail");

			try{
				for (int i = 0; i < list.size(); i++) {
					MailingSystem mailingSystem = (MailingSystem) ActionContext.getContext().getApplication().get(AdminConstants.MAILING_OPTIONS);
					MailHelper mailHelper = new MailHelper(mailingSystem.getServerAddress(), mailingSystem.getServerUsername(), mailingSystem.getServerPassword());

					StringBuffer URL = new StringBuffer();
					URL.append(CommonUtil.getLocalBasePath(request));
					URL.append("/mail/registerationConfirmationMail.htm?id=").append(list.get(i).getUserId()).append("&pass=").append(list.get(i).getPassword()).append("&uname=").append(list.get(i).getFirstName()).append("%20").append(list.get(i).getLastName()).append("&link=").append(list.get(i).getActivationKey());

					String message = CommonUtil.getURLContent(URL.toString());
					mailHelper.sendMail(mailingSystem.getAccountsConfirmationEmail(), list.get(i).getEmail(), "Account Confirmation", message);
				}
			}catch (Exception e) {
				e.printStackTrace();
				addActionError("Error while resend registeration confirmation mail");
			}
			pendingCount = list.size();
		}

		request.getSession().setAttribute("sendConfirmationMail", null);

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getPendingCount() {
		return pendingCount;
	}

	public void setPendingCount(int pendingCount) {
		this.pendingCount = pendingCount;
	}
}